#!/bin/bash

# 检查Redis服务
echo "检查Redis服务..."
if ! command -v redis-cli &> /dev/null; then
    echo "Redis未安装，请先安装Redis"
    echo "Ubuntu/Debian: sudo apt-get install redis-server"
    echo "CentOS/RHEL: sudo yum install redis"
    echo "Mac: brew install redis"
    exit 1
fi

# 尝试连接Redis
echo "尝试连接Redis..."
if ! redis-cli ping &> /dev/null; then
    echo "Redis服务未启动，请先启动Redis服务"
    echo "Linux: sudo systemctl start redis 或 sudo service redis-server start"
    echo "Mac: brew services start redis"
    exit 1
fi

# 检查前端构建
echo "检查前端构建..."
if [ ! -d "frontend/dist" ]; then
    echo "前端未构建，正在构建前端..."
    
    # 检查是否安装了npm
    if ! command -v npm &> /dev/null; then
        echo "未安装npm，请先安装Node.js和npm"
        exit 1
    fi
    
    # 安装前端依赖并构建
    cd frontend
    npm install
    npm run build
    cd ..
    
    if [ ! -d "frontend/dist" ]; then
        echo "前端构建失败，请检查错误信息"
        exit 1
    fi
    
    echo "前端构建成功"
fi

# 检查Python依赖
echo "检查Python依赖..."
if ! command -v pip &> /dev/null; then
    echo "未找到pip，请先安装Python和pip"
    exit 1
fi

# 安装Python依赖
pip install -r requirements.txt

# 启动系统
echo "启动系统..."
python app.py 